package com.education.ai.repository;

import com.education.ai.entity.TeachingResource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * 教学资源数据访问接口
 */
@Repository
public interface TeachingResourceRepository extends JpaRepository<TeachingResource, Long> {

    /**
     * 根据资源类型查询资源
     */
    List<TeachingResource> findByType(String type);

    /**
     * 根据学科查询资源
     */
    List<TeachingResource> findBySubject(String subject);

    /**
     * 根据学科和年级查询资源
     */
    List<TeachingResource> findBySubjectAndGrade(String subject, String grade);

    /**
     * 根据标题模糊查询资源
     */
    List<TeachingResource> findByTitleContaining(String titlePart);

    /**
     * 根据知识点查询资源
     */
    @Query("SELECT tr FROM TeachingResource tr WHERE tr.knowledgePoints LIKE %:knowledgePoint%")
    List<TeachingResource> findByKnowledgePoint(@Param("knowledgePoint") String knowledgePoint);

    /**
     * 根据标签查询资源
     */
    @Query("SELECT tr FROM TeachingResource tr WHERE tr.tags LIKE %:tag%")
    List<TeachingResource> findByTag(@Param("tag") String tag);

    /**
     * 查询使用次数最多的资源
     */
    @Query("SELECT tr FROM TeachingResource tr ORDER BY tr.usageCount DESC")
    List<TeachingResource> findTopByUsageCount(Pageable pageable);

    /**
     * 查询评分最高的资源
     */
    @Query("SELECT tr FROM TeachingResource tr WHERE tr.rating IS NOT NULL ORDER BY tr.rating DESC")
    List<TeachingResource> findTopByRating(Pageable pageable);
} 